#include "stdafx.h"
#include "card.h"

const char Card::_Face[13] = { 'A' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '1' , 'J' , 'Q' , 'K' } ;
const char Card::_Suit[6] = { 0x03 , 0x04 , 0x05 , 0x06 , 0x01 , 0x02 } ;

Card::Card()
{
	this->Create() ;
	this->Clear() ;	
}

Card::Card( int value, char suit )
{
	this->Create() ;
	this->Set( value , suit ) ;
}

Card::Card( const Card & obj )
{
	this->Create() ;
	this->Set( obj ) ;
}

Card::~Card()
{	
	if( this->face !=NULL )
	{
		delete[] this->face ;
		this->face = NULL ;
	}
}

void Card::Create() 
{
	this->face = new char[4] ;
}

void Card::Clear()
{
	this->value = EMPTY ;
	this->suit = EMPTY ;
}

void Card::swap( int & value )
{
	this->value ^= value ;
	value ^= this->value ;
	this->value ^= value;
}

void Card::swap( char & suit )
{
	this->suit ^= suit ;
	suit ^= this->suit ;
	this->suit ^= suit;
}

void Card::putSuit() 
{
	for( int i = 0 , n = sizeof( this->face ) - 1 ; i < n ; ++i )
	{
		if( this->face[i] == EMPTY )
		{
			this->face[i] = this->suit ;
			return ;
		}
	}
}

void Card::Swap( Card & obj ) 
{
	this->swap( obj.value ) ;
	this->swap( obj.suit ) ;
}

const int& Card::getValue()
{
	return this->value ;
}

const char& Card::getSuit()
{
	return this->suit ;
}

const int& Card::Value() 
{
	return this->getValue() ;
}

const char& Card::Suit()
{
	return this->getSuit() ;
}

const char* Card::Face()
{
	return this->getFace() ;
}

const char* Card::setFaceByValue()
{
	memset( this->face , EMPTY , sizeof( this->face ) ) ;
	this->face[0] = this->getFaceByValue( this->value ) ;
	if( this->face[0] == '1' )
		this->face[1] = '0' ;
	return this->face ;
}

char Card::getFaceByValue( int value ) const
{
	if( this->Validate( value ) )
		return this->_Face[ value - 1 ] ;
	return EMPTY ;
}

int Card::getValueByFace( char face ) const
{
	for ( int  i =  0 , n = sizeof( this->_Face ) ;  i < n ; ++i )
	{
		if ( this->_Face[i] == face )
			return ( i + 1 ) ;
	}
	return EMPTY ;
}

const char* Card::getFace()
{
	this->setFaceByValue() ;
	this->putSuit() ;
	return this->face ;
}

Card& Card::operator = ( const Card & obj )
{
	if ( this != &obj )
	{
		this->value = obj.value ;
		this->suit = obj.suit ;
	}
	return *this ;
}

Card& Card::Set( int value ) 
{
	if( this->Validate( value ) )
		this->value = value ;
	return *this ;
}

Card& Card::Set( char suit ) 
{
	if ( this->Validate( suit ) )
	{
		this->suit = this->_Suit[ suit ] ;
		if( suit > 3 )
			this->value = this->suit + 13 ; 
	}	
	return *this ;
}

Card& Card::Set( int value, char suit )
{	
	if( this->Validate( value , suit ) )
	{
		this->value = value ;
		this->suit = this->_Suit[ suit ] ;
		if( suit > 3 )
			this->value = this->suit + 13 ; 
	}
	return *this ;
}

Card& Card::Set( const Card & obj )
{
	return *this = obj ;
}

bool Card::Empty() const 
{
	return( this->value == EMPTY && this->suit == EMPTY ) ;
}

bool Card::Equals( int value ) const
{
	return this->value == value ;
}

bool Card::Equals( char suit ) const
{
	return this->suit == suit ;
}

bool Card::Equals( int value , char suit ) const 
{
	return ( this->value == value ) && ( this->suit == suit ) ;
}

bool Card::Equals( Card & obj ) const
{
	return this->Equals( obj.value , obj.suit ) ;
}

bool Card::Validate( int value ) const
{
	return ( value > 0  && value < 14 ) ;
}

bool Card::Validate( char suit ) const
{
	return ( suit > -1 && suit < 6 ) ;
}

bool Card::Validate( int value , char suit ) const
{
	return ( value > 0  && value < 14 ) && ( suit > -1 && suit < 6 ) ;
}

bool Card::Validate( Card& obj ) const
{
	return this->Validate( obj.value, obj.suit ) ;
}

const bool Card::operator > ( const Card & obj ) 
{
	return this->value > obj.value ;
}

const bool Card::operator < ( const Card & obj ) 
{
	return this->value < obj.value ;
}

const bool Card::operator == ( const Card & obj ) 
{
	return ( this->value == obj.value && this->suit == obj.suit ) ;
}

const bool Card::operator != ( const Card & obj ) 
{
	return !( *this == obj ) ;
}